//
//  WPPDFViewController.m
//  PDF分页展示
//
//  Created by GZCP1897 on 17/1/16.
//  Copyright © 2017年 GZCP1897. All rights reserved.
//

#import "WPPDFViewController.h"
#import "WPPDFView.h"

@interface WPPDFViewController ()

@property(nonatomic, assign)NSUInteger numberOfPages;
@property(nonatomic, assign)CGPDFDocumentRef pdfDocRef;

@property(nonatomic, weak)WPPDFView *pdfView;
@end

@implementation WPPDFViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.view.backgroundColor = [UIColor redColor];
//
//    NSString *fileName = @"example_pdf";//Book1  example_pdf
//
//    NSString * pdfPath = [[NSBundle mainBundle] pathForResource:fileName ofType:@"pdf"];
//    CGPDFDocumentRef pdfDocRef = [self PDFDocumentRefWithFilePath:pdfPath];
//
//    self.pdfDocRef = pdfDocRef;
//    CGPDFPageRef pdfPageRef = CGPDFDocumentGetPage(pdfDocRef, 1);
//    self.pdfPageRef = pdfPageRef;
    
    WPPDFView *pdfPageView = [[WPPDFView alloc] init];
//    pdfPageView.pdfDocRef = pdfDocRef;
//    pdfPageView.pdfPageRef = pdfPageRef;
    self.pdfView = pdfPageView;
    
    UIButton *next = [[UIButton alloc] init];
    next.layer.borderColor = [UIColor blueColor].CGColor;
    next.layer.borderWidth = 2.0;
    [next setTitle:@"next" forState:UIControlStateNormal];
    [next setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [next addTarget:self action:@selector(nextClick:) forControlEvents:UIControlEventTouchUpInside];
    next.bounds = CGRectMake(0, 0, 100, 50);
    next.center = self.view.center;
    
    
    self.view = pdfPageView;
    [self.view addSubview:next];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
}

- (void)nextClick:(UIButton *)btn{
//    int tmp = arc4random() %22 + 1;
//
//    CGPDFPageRef page = CGPDFDocumentGetPage(self.pdfDocRef, tmp);
//    self.pdfView.pdfPageRef = page;
    [self.pdfView setNeedsDisplay];
    
    //4     CGPDFPage 0x60400011d130
    
    //5     CGPDFPage 0x60400011a0d0
    //7     CGPDFPage 0x600000115690
    //20    CGPDFPage 0x6000003027f0
}






/**
 根据项目中文件路径创建PDFDocumentRef

 @param filePath 文件路径
 @return PDFDocumentRef
 */
- (CGPDFDocumentRef)PDFDocumentRefWithFilePath:(NSString *)filePath{
    
    CFStringRef path;
    CFURLRef url;
    CGPDFDocumentRef document;
    
    path = CFStringCreateWithCString(NULL, [filePath UTF8String], kCFStringEncodingUTF8);
    url = CFURLCreateWithFileSystemPath(NULL, path, kCFURLPOSIXPathStyle, NO);
    CFRelease(path);
    
    document = CGPDFDocumentCreateWithURL(url);
    CFRelease(url);
    
    self.numberOfPages = CGPDFDocumentGetNumberOfPages(document); // 设置PDF文件总页数
    NSLog(@"totalPages = %ld", self.numberOfPages);
    if (self.numberOfPages == 0) { // 创建出错处理
        NSLog(@"Create Error");
        return NULL;
    }
    return document;
}



- (void)dealloc{
    CGPDFDocumentRelease(_pdfDocRef);
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
